<?php

namespace App\Http\Admin\Controller\Auth;

use App\Http\Admin\Middleware\AuthMiddleware;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Request\auth\MenuRequest;
use App\Request\auth\Service\Auth\MenuService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use function App\Helper\listToTree;

#[Middlewares([
    AuthMiddleware::class,
])]
#[Controller(prefix: '/adminapi/auth/menu/')]
final class MenuController extends AbstractController
{

    public function __construct(
        protected readonly MenuService $service
    )
    {
    }

    #[GetMapping("lists")]
    public function lists(RequestInterface $request): Result
    {
        $data = $this->service->getList($request->all());
        return $this->success(listToTree($data->toArray(), 0));
    }

    #[GetMapping('parentLists')]
    public function parentLists(): Result
    {
        return $this->success($this->service->getList(['parent_id' => 0,'status'=>1]));
    }

    #[PostMapping('create')]
    public function create(MenuRequest $request): Result
    {
        return $this->success($this->service->create($request->all()));
    }

    #[PutMapping('update')]
    public function update(MenuRequest $request): Result
    {
        $this->service->updateById($request->input('id'), $request->all());
        return $this->success([], '更新成功');
    }

    #[DeleteMapping('delete')]
    public function delete(RequestInterface $request): Result
    {
        $this->service->deleteById($request->input('id'));
        return $this->success([], '删除成功');
    }

}